﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LyricsFinder;
using iTunesLib;
using System.IO;
using System.Drawing.Drawing2D;

namespace iTunesKannadaLyrics
{
    public partial class MainForm : Form
    {
        private int width;
        private int height;

        iTunesApp app = new iTunesAppClass();

        public MainForm()
        {   
            InitializeComponent();

            app.OnPlayerPlayingTrackChangedEvent += new _IiTunesEvents_OnPlayerPlayingTrackChangedEventEventHandler(app_OnPlayerPlayingTrackChangedEvent);
            app.OnPlayerPlayEvent += new _IiTunesEvents_OnPlayerPlayEventEventHandler(app_OnPlayerPlayEvent);

        }

        void app_OnPlayerPlayEvent(object iTrack)
        {
            Refresh(iTrack);
        }

        void app_OnPlayerPlayingTrackChangedEvent(object iTrack)
        {
            Refresh(iTrack);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            lblSongInfo.Text = "";
            lblArtistInfo.Text = "";
            lblAlbumInfo.Text = "";
            lblGenreInfo.Text = "";

            Refresh(app.CurrentTrack);

            width = this.Width;
            height = this.Height;

            timer.Start();
        }

        public SongInfo GetSongInfo(object iTrack)
        {
            SongInfo song = null;

            try
            {
                if (null != app)
                {
                    IITTrack track = (IITTrack)iTrack;

                    if (null != track)
                    {
                        song = new SongInfo();

                        song.SongName = track.Name;
                        song.Artist = track.Artist;
                        song.Album = track.Album;
                        song.Genre = track.Genre;
                        IITArtworkCollection artworks = track.Artwork;

                        if (null != artworks && artworks.Count > 0)
                        {
                            IITArtwork artwork = artworks[1];
                            artwork.SaveArtworkToFile("c:\\temp\\Album.jpg");
                            Stream r = File.Open("c:\\temp\\Album.jpg", FileMode.Open);
                            song.AlbumArt = Image.FromStream(r);
                            r.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return song;
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            grpLyrics.Width += this.Width - width;
            grpLyrics.Height += this.Height - height;

            grpSongInfo.Width += this.Width - width;
            grpSongInfo.Height += this.Height - height;

            width = this.Width;
            height = this.Height;

        }

        private void Refresh(object iTrack)
        {
            try
            {
                SongInfo song = GetSongInfo(iTrack);

                if (null != song && lblSongInfo.Text != song.SongName)
                {
                    lblSongInfo.Text = song.SongName;
                    lblArtistInfo.Text = song.Artist;
                    lblAlbumInfo.Text = song.Album;
                    lblGenreInfo.Text = song.Genre;
                    pbAlbumArt.Image = song.AlbumArt;

                    SetImage(pbAlbumArt);

                    LyricsFinder.LyricsFinder lyricsFinder = new LyricsFinder.LyricsFinder();
                    webBrowser.DocumentText = lyricsFinder.GetLyrics(song.SongName, song.Album, song.Genre);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
        {    //double to hold the final multiplier to use when scaling the image    
            double multiplier = 0;

            //string for holding layout    
            string layout;

            //determine if it's Portrait or Landscape        
            if (currH > currW) layout = "portrait";
            else layout = "landscape";
            switch (layout.ToLower())
            {
                case "portrait":
                    //calculate multiplier on heights            
                    if (destH > destW)
                    {
                        multiplier = (double)destW / (double)currW;
                    }
                    else
                    {
                        multiplier = (double)destH / (double)currH;
                    }
                    break;

                case "landscape":
                    //calculate multiplier on widths            
                    if (destH > destW)
                    {
                        multiplier = (double)destW / (double)currW;
                    }
                    else
                    {
                        multiplier = (double)destH / (double)currH;
                    }
                    break;
            }
            //return the new image dimensions    
            return new Size((int)(currW * multiplier), (int)(currH * multiplier));
        }

        //Resize the image
        public void SetImage(PictureBox pb)
        {
            try
            {

                //create a temp image        
                Image img = pb.Image;

                //calculate the size of the image        
                Size imgSize = GenerateImageDimensions(img.Width, img.Height, this.pbAlbumArt.Width, this.pbAlbumArt.Height);

                //create a new Bitmap with the proper dimensions        
                Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height);

                //create a new Graphics object from the image        
                using (Graphics gfx = Graphics.FromImage(img))
                {
                    //clean up the image (take care of any image loss from resizing)        
                    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                }
                //empty the PictureBox        
                pb.Image = null;

                //center the new image        
                pb.SizeMode = PictureBoxSizeMode.CenterImage;

                //set the new image        
                pb.Image = finalImg;
            }
            catch (System.Exception e)
            {
                //MessageBox.Show(e.Message);    
            }
            
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            Refresh(app.CurrentTrack);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (AboutBox aboutBox = new AboutBox())
            {
                aboutBox.ShowDialog();
            }
        }
    }
}
